/**
 * 
 */
package systole.view.crud.patient.patientComponents.sugeries;

import java.awt.Frame;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.Surgery;
import systole.domain.clinicalInformation.SurgeryPatient;
import systole.domain.clinicalInformation.SurgeryPatientId;

import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.view.crud.patient.patientComponents.controller.ControllerEditionPatienComponent;

/**
 * @author jmj
 *
 */
public class ControllerEditionSugeryByPatient extends ControllerEditionPatienComponent {

    /**
     * @param patient
     */
    public ControllerEditionSugeryByPatient(Patient patient) {
        super(patient);
        this.entityName = "Cirugía";
        this.newDescription = "Nueva";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        SurgeryPatient surgeryPatient = (SurgeryPatient) this.curretnEntity;
        JDialogSugeries form = (JDialogSugeries) this.editForm;
        surgeryPatient.setPatient(this.patient);
        surgeryPatient.setSurgery((Surgery) form.getCmbSurgeries().getSelectedItem());
        Integer age = null;
        try {
            age = Integer.parseInt(form.getjTxtAge().getText());
        } catch (NumberFormatException e) {
        }
        surgeryPatient.setAgeAtSurgery(age);
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        SurgeryPatient surgeryPatient = (SurgeryPatient) this.curretnEntity;
        JDialogSugeries form = (JDialogSugeries) this.editForm;
        form.getCmbSurgeries().setSelectedItem(surgeryPatient.getSurgery());
        form.getjTxtAge().setText(surgeryPatient.getAgeAtSurgery() != null ? surgeryPatient.getAgeAtSurgery().toString() : "");
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#save()
     */
    @Override
    protected void save() {
        if (!this.isEditing()) {
            SurgeryPatient surgeryPatient = (SurgeryPatient) this.curretnEntity;
            surgeryPatient.setId(new SurgeryPatientId(surgeryPatient.getPatient().getId(), surgeryPatient.getSurgery().getId()));
            this.patient.getSurgeriesPatient().add(surgeryPatient);
        }
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        JDialogSugeries form = (JDialogSugeries) this.editForm;

        if ((!this.isEditing()) && ((form.getCmbSurgeries().getSelectedIndex() == -1) || (form.getCmbSurgeries().getSelectedItem() == null))) {
            return "Debe seleccionar una cirugía";
        }

        if (form.getjTxtAge().getText() == null) {
            return "Debe ingresar la edad a la que se realizó la cirugía";
        }
        return null;
    }

    @Override
    protected void loadIconOnForm() {
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new SurgeryPatient();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogSugeries(parent, this);
    }

    @Override
    protected void initFormsControls() throws ExceptionDAO {
        JDialogSugeries form = (JDialogSugeries) this.editForm;
        form.getCmbSurgeries().removeAllItems();
        List<Surgery> list = this.facadeDB.getSurgeryBroker().getAllSurgery();
        Iterator<Surgery> it = list.iterator();
        while (it.hasNext()) {
            form.getCmbSurgeries().addItem(it.next());
        }
        form.getCmbSurgeries().setEnabled(!this.isEditing());
        this.editForm = form;
    }
}
